<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //test()方法---t/f(修饰符带来的影响)
        var pattern = /box/; //区分大小写 每次从头开始匹配
        var str = "This is a box!";
        console.log(pattern.test(str)); //匹配成功返回true  匹配不成功返回false // true
        var str = "This is a Box!"; 
        console.log(pattern.test(str));//false
        var str = "This is a Box!,box"; 
        console.log(pattern.test(str));//true

        var pattern = /box/i; //不区分大小写
        var str = "This is a Box!"; 
        console.log(pattern.test(str));//true

        var regExp = /a/i;  //没有g每次都是从头开始
        console.log(regExp.test("ab"));//true
        console.log(regExp.test("ab")); //true
        console.log(regExp.test("ab"));//true
        console.log(regExp.test("ab"));//true

        //思考如果加了g，循环了若干次后是true还是false，这是为什么？test中的lastIndex
        var regExp = /a/gi;
        console.log(regExp.test("ab"),regExp.lastIndex);//true 1下次从b开始匹配
        console.log(regExp.test("ab"),regExp.lastIndex);//false 0为什么？
        console.log(regExp.test("ab"),regExp.lastIndex);//true 1
        console.log(regExp.test("ab"),regExp.lastIndex);//false 0为什么？

        //案例：结合定位匹配
        console.log(/oo/.test("moon")); //t
        console.log(/oo\b/.test("moon")); //f
        console.log(/oon\b/.test("moon")); //t
        console.log(/\boo/.test("moon")); //f

        console.log(/oo\B/.test("moon")); //t
        console.log(/oon\B/.test("moon")); //f
        console.log(/oo\B/.test("moon")); //t
        console.log(/\Boo\B/.test("moon")); //t


        // exec()方法 精确的匹配到所要的字符串
        // 返回的是一个数组 
        var pattern = /box/ig;
        var str = "This is a Box!This is a box!";
        console.log(pattern.exec(str),pattern.lastIndex); //'Box' 13
        console.log(pattern.exec(str),pattern.lastIndex); //'box' 27
        console.log(pattern.exec(str),pattern.lastIndex); //null 0
        console.log(pattern.exec(str),pattern.lastIndex); //'Box' 13
        console.log(pattern.exec(str),pattern.lastIndex); //'box' 27
        // 案例
        var pattern = /box/ig;
        var str = "This is a Box!This is a box!";
        while (match = pattern.exec(str)) {
            console.log(match[0]);
        } //Box box


        //替换——返回替换后的结果
        //字符串替换
        var str = "This is a box This is a box!";
        console.log(str); //This is a box This is a box!
        console.log(str.replace("This", "1111")); //1111 is a box This is a box!

        //正则表达式替换
        var pattern = /This/;
        // var pattern = /(?!^)This/g;//只匹配后边的This
        // var pattern = /(?=^)This/g;//只匹配前边的This
        var str = "This is a box This is a box!";
        console.log(str); //This is a box This is a box!
        console.log(str.replace(pattern, "1111")); //1111 is a box This is a box!

        //案例 结合后续正则知识，可进行更为复杂的替换，具体参见后续练习
        console.log("sdafsa sdfea2s".replace(/a\ds/g,"*")); //sdafsa sdfe* \d数组
        console.log("sdafsa sdfea2s".replace(/a\Ds/g,"*")); //sd**dfea2s  \D非数字
        console.log("sdafsa sdfea2s".replace(/a\ws/g,"*")); //sd*a sdfe*  \w 除空格以外的字符(数字也是)
        console.log("sdafsa sdfea2s".replace(/a\Ws/g,"*")); //sdafs*dfea2s \W 空格字符

        //match()方法返回匹配的子串 字符串.match(正则) 7
        //search()方法  匹配到一次就结束 8
        //split()方法 9
    </script>
</body>
</html>